<?php
$this->breadcrumbs=array(
    Yii::t('site','Admin') => array('/admin'),
    Yii::t('site','Auth Items'),
);

$this->menu=array(
    array('label'=>Yii::t('site','Create New Auth Item'), 'url'=>array('create')),
);
?>

<h2><?php echo Yii::t('site','Manage Authorizations') ?></h2>

<?php echo CHtml::form(null,'get'); ?>
    <?php echo CHtml::hiddenField('r', '/admin/auth/admin' ); ?>
    <?php echo Yii::t('site','Filter:') ?>&nbsp;
    <?php echo CHtml::dropDownList('filter', $filter, array('0,1,2'=>'(Select)','2'=>Yii::t('site','Roles'),'0,2'=>Yii::t('site','Roles,Operations')),array('submit'=>'')); ?>
</form>

<table class="dataGrid">
    <thead>
        <tr>
            <th><?php echo CHtml::encode(Yii::t('site','Name')) ?></th>
            <th><?php echo CHtml::encode(Yii::t('site','Type')) ?></th>
            <th><?php echo CHtml::encode(Yii::t('site','Description')) ?></th>
            <th><?php echo CHtml::encode(Yii::t('site','Action')) ?></th>
        </tr>
    </thead>
    <tbody>
        <?php $c=0;foreach( $roles as $n=>$role ) : ?>
        <?php echo getRow($role, $c); ?>
        <?php endforeach; ?>
    </tbody>
</table>

<?php
function getRow( $role, &$c )
{
    $types = array(
        0 => '&nbsp;&nbsp;&nbsp;&nbsp;'.Yii::t('site','Operation'),
        1 => '&nbsp;&nbsp;'.Yii::t('site','Task'),
        2 => Yii::t('site','Role')
    );
    $pad            = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $role['level']);
    $class          = $c%2 ? 'even' : 'odd';
    $link_edit      = CHtml::link(CHtml::encode(Yii::t('site','Edit')),array('edit','name'=>$role['name']));
    $link_copy      = CHtml::link(CHtml::encode(Yii::t('site','Copy')),array('create','from_name'=>$role['name']));
    $link_add_child = CHtml::link(CHtml::encode(Yii::t('site','Add Child')),array('create','parent'=>$role['name']));
    $link_delete    = CHtml::linkButton(
        Yii::t('site','Delete'),
        array(
      	    'submit'=>array('auth/delete','id'=>$role['name']),
      	    'confirm'=>sprintf(Yii::t('site',"Are you sure to delete Auth Item %s?"),$role['name'])
        )
    );
    $tmp = <<< EOL
        <tr class="{$class}">
            <td>{$pad}<strong>{$role['name']}</strong></td>
            <td>{$types[$role['type']]}</td>
            <td>{$role['description']}</td>
            <td class="centered">{$link_edit} {$link_copy} {$link_add_child}<br/>{$link_delete}</td>
        </tr>
EOL;
    $c++;
    if(isset( $role['children'] ) ) {
        foreach( $role['children'] as $n=>$child_role ) {
            $tmp .= getRow( $child_role, $c );
        }
    }
    return $tmp;
}
?>
